package com.zhuhai.controller;


import com.zhuhai.service.PassengerService;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 旅客Controller.
 *
 *  @author zhuhai
 *  @version 1.0.0.0
 */
@Controller
@RequestMapping(value = "passenger")
public class PassengerController {
    @Resource
    private PassengerService passengerService;

    /**
     * 用简单查询通过旅客姓名（模糊匹配）和证件号（全等）查询旅客信息，需支持分页，并且根据创建时间倒叙排序。
     * http://localhost:8080/passenger/list?name=王&cardNum=210102198604101018
     * @param name
     * @param cardNum
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping(path = "/list")
    @ResponseBody
    public Object getAllPassenger(@RequestParam final String name,
                                  @RequestParam final String cardNum,
                                  @RequestParam(defaultValue = "0") final int page,
                                  @RequestParam(defaultValue = "10") final int pageSize) {
        final Pageable pageable = PageRequest.of(page, pageSize);
        return passengerService.getPassengerList(name, cardNum, pageable);
    }

    /**
     * 用简单查询通过旅客姓名（模糊匹配）和证件号（全等）查询旅客信息，需支持分页，并且根据创建时间倒叙排序。
     * http://localhost:8080/passenger/getIssueOfficeCodeAndType?issueOfficeCode=LUM555&type=CHD&page=1&pageSize=10
     * @param issueOfficeCode
     * @param type
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping(path = "/getIssueOfficeCodeAndType")
    @ResponseBody
    public Object findByIssueOfficeCodeAndType(@RequestParam final String issueOfficeCode,
                                                @RequestParam final String type,
                                                @RequestParam(defaultValue = "0") final int page,
                                                @RequestParam(defaultValue = "10") final int pageSize) {
        final Pageable pageable = PageRequest.of(page, pageSize);
        return passengerService.findByIssueOfficeCodeAndType(issueOfficeCode, type, pageable);
    }

    /**
     * 使用@Query注解及jpql语句查询出客票低于500的旅客记录，要求使用“countQuery”,返回Page对象，同时使用Sort排序方式根据旅客年龄升序排序。
     * http://localhost:8080/passenger/getListByPrice?price=500&page=2&pageSize=20
     * @param price
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping(path = "/getListByPrice")
    @ResponseBody
    public Object getPassengerListByPrice(@RequestParam final BigDecimal price,
                                          @RequestParam(defaultValue = "0") final int page,
                                          @RequestParam(defaultValue = "10") final int pageSize) {
        final Sort sort = new Sort(Sort.Direction.DESC, "birthdate");
        final Pageable pageable = PageRequest.of(page, pageSize, sort);
        return passengerService.getPassengerListByPriceLessThanEqual(price, pageable);
    }
}
